package com.zhengxyou.demoscrolltab

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import com.zhengxyou.demoscrolltab.databinding.FragmentImageBinding

class ImageViewFragment : BaseFragment() {
    private var _binding: FragmentImageBinding? = null
    private val binding
        get() = _binding!!

    override fun scrollCallback(scrollY: Int, dy: Int) {
        (requireActivity() as MainActivity).scrollCallback(scrollY,dy)
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentImageBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.nsScroll.setOnScrollChangeListener(object :
            NestedScrollView.OnScrollChangeListener {
            override fun onScrollChange(
                v: NestedScrollView,
                scrollX: Int,
                scrollY: Int,//当前垂直滑动的距离,头部的时候是0，当前滚动条的位置
                oldScrollX: Int,
                oldScrollY: Int//上一次停止后垂直滑动的距离
            ) {
//                Log.d("TAG", "scrollY:$scrollY,oldScrollY:$oldScrollY")
//                if (scrollY > oldScrollY) {
//                    //上滑
//                    scrollCallback(scrollY,scrollY-oldScrollY)
//                } else {
//                    //下滑
//                    scrollCallback(scrollY,scrollY-oldScrollY)
//                }
                scrollCallback(scrollY, scrollY - oldScrollY)
            }

        })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        fun newInstance(): Fragment {
            val fragment = ImageViewFragment()
            return fragment
        }
    }
}